package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.entity.refund.RefundVO;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.rest.refund.QuerryRefundData;
import com.burgeon.backend.SHOPBACKEND.service.BackendRefundService;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class BackendRefundServiceImpl implements BackendRefundService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    @Override
    public HashMap getData(String account, Integer start) {
        HashMap map = new HashMap();
        Info info = GetInfo(account);
        QuerryRefundData querryRefundData = new QuerryRefundData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            ArrayList<RefundVO> data = querryRefundData.getData(start);
            map.put("count", data.get(0).getCount());
            for (RefundVO refundVO : data) {
                Goods goods = backendGoodsMapper.findGoodsDataByCode(refundVO.getGoodsCode(), account);
                if (null != goods) {
                    refundVO.setGoodsImg(goods.getImg());
                }

                User user = userMapper.FindUserDataByPhone(refundVO.getVipPhone(), account);
                if (null != user) {
                    refundVO.setVipImg(user.getImg());
                }
                refundVO.setCount(null);
            }
            map.put("data", data);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    private Info GetInfo(String token) {
        return accountMapper.GetAccData(token);
    }
}
